<template>
  <el-dialog title="制造商查询" :close-on-click-modal="false" :visible.sync="visible" fullscreen append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
      <div class="text item">
        <el-form inline size="mini">
          <el-form-item label="制造商编码">
            <el-input v-model="dataForm.code"></el-input>
          </el-form-item>
          <el-form-item label="制造商名称">
            <el-input v-model="dataForm.name"></el-input>
          </el-form-item>
          <el-form-item label="商品类别" prop="ctgId">
            <el-select v-model="dataForm.ctgId" placeholder="请选择">
              <el-option
                v-for="item in ctgOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="searchHandle" size="mini">查询</el-button>
            <el-button icon="el-icon-delete" type="info" @click="clearSearchForm" size="mini">清空</el-button>
          </el-form-item>
        </el-form>
      </div>

    <el-table :data="dataList" height="calc(100vh - 215px)" ref="singleTable" border v-loading="dataListLoading">
      <el-table-column type="index" width="50">
        <template slot-scope="scope">
          <el-radio v-model="dataForm.supplyId" :label="scope.row.id" style="position: relative;left: 6px;top:-2px;" @change="checkSupply(scope.row)">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column prop="code" header-align="center" align="center" label="制造商编码"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="制造商名称"></el-table-column>
      <el-table-column prop="ctgNameList" header-align="center" align="center" label="商品类别"></el-table-column>
    </el-table>
    <div class="avue-crud__pagination">
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        background
        layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
  </el-dialog>
</template>

<script>
  import {fetchCustomerList} from '@/api/eims/customer.js'
  import { fetchSupplyList } from '@/api/eims/supply.js'
  import { getCtgList } from '@/api/eims/ctg.js'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          supplyId: '',
          code: '',
          name: '',
          ctgId: ''
        },
        dataList: [],
        ctgOptions:[],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0
      }
    },
    methods: {
      init(data) {
        this.visible = true
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
        this.dataForm.ctgId = data.ctgId || ''
        this.getSupplyList()
        this.getCtgListOption()
      },
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getSupplyList()
      },
      //  清空搜索表单
      clearSearchForm(){
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
      },
      getSupplyList() {
        let params = {current: this.pageIndex,size: this.pageSize}
        fetchSupplyList(Object.assign(params,this.dataForm)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getSupplyList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getSupplyList()
      },
      // 选择制造商
      checkSupply(val) {
        this.visible = false
        this.$emit('refreshData',val)
      },
      // 获取商品类别列表
      getCtgListOption () {
          getCtgList().then(response => {
            if(response.data.code == '0'){
              let ctgData = response.data.data
              let list = []
              ctgData.forEach(item => {
                  let ctgObj = {value: item.id,label: item.name}
                  list.push(ctgObj)
              })
              this.ctgOptions = list
            }else{
              this.$message.error(response.data.msg)
            }
          })
      }
    }
  }
</script>

<style>
</style>
